// gulp 中使用异步任务的几种方式

const fs = require('fs')

// 回调函数方式
exports.callback = done => {
    console.log('callback task~')
    done()
}

exports.callback_error = done => {
    console.log('callback_error task~')
    done(new Error('task failed'))
}

// Promise 方式

exports.promise = () => {
    console.log('promise task')
    return Promise.resolve()
}

exports.promise_reject = () => {
    console.log('promise_reject task')
    return Promise.reject(new Error('task failed'))
}

// async await
const timeout = time => {
    return new Promise(resolve => {
        setTimeout(resolve, time)
    })
}
exports.async = async () => {
    await timeout(1000)
    console.log('async task~')
}


// stream 流方式
exports.stream = () => {
    const read = fs.createReadStream('package.json')
    const write = fs.createWriteStream('tmp.txt')
    read.pipe(write)
    return read
}
